#pragma once
#include<unordered_map>
#include<mutex>
#include"util.hpp"
#include"logger.hpp"
class online_manager
{
    public:
    online_manager()
    {}
    //登陆成功后加入管理
    void enter_game_hall(uint64_t uid, wsserver_t::connection_ptr &conn)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _hall_user.insert(std::make_pair(uid,conn));
    }
    void enter_game_room(uint64_t uid,   wsserver_t::connection_ptr &conn)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _room_user.insert(std::make_pair(uid,conn));
    }
    //退出登录后删除用户
    void exit_game_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _hall_user.erase(uid);
    }
    void exit_game_room(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _room_user.erase(uid);
    }
    //判断用户是否在游戏大厅或对战房间中
    bool is_in_game_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto pos= _hall_user.find(uid);
        if(pos==_hall_user.end())
        {
            return false;
        }
        return true;
    }
    bool is_in_game_room(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto pos= _room_user.find(uid);
        if(pos==_room_user.end())
        {
            return false;
        }
        return true;
    }
    //根据用户id获取对应的连接信息
    wsserver_t::connection_ptr get_conn_from_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto pos= _hall_user.find(uid);
        if(pos==_hall_user.end())
        {
            return nullptr ;
        }
        return pos->second;
    }
    wsserver_t::connection_ptr get_conn_from_room(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto pos= _room_user.find(uid);
        if(pos==_room_user.end())
        {
            return nullptr ;
        }
        return pos->second;
    }
    private:
    //管理在游戏大厅的用户
    std::unordered_map<uint64_t,wsserver_t::connection_ptr> _hall_user;
    //管理在对战房间中的用户
    std::unordered_map<uint64_t,wsserver_t::connection_ptr> _room_user;
    std::mutex _mutex;
};